﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using PlanningGIS.Framework;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Controls;
using PlanningGIS.Win.Controls;
using PlanningGIS.ArcGIS;

namespace PlanningGIS.SelectQuery
{
    /// <summary>
    /// 作用：全文检索Command类
    ///  </summary>
    public partial class FrmQueryAllLayer : Office2007Form
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmQueryAllLayer()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        private static List<string> ValueList = new List<string>();
        /// <summary>
        /// 
        /// </summary>
        private bool bStop = false;
        /// <summary>
        /// 
        /// </summary>
        private List<IFeatureLayer> m_FeatureLayerList = new List<IFeatureLayer>();
        /// <summary>
        /// 
        /// </summary>
        IApplication m_Application = null;
        /// <summary>
        /// 
        /// </summary>
        public IApplication Application
        {
            set
            {
                m_Application = value;
            }
        }
        
        /// <summary>
        /// 进行全文搜索函数，如果搜索结果记录超过500条，则停止搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Search_Click(object sender, EventArgs e)
        {
            bStop = false;
            int ResultCount = 0;    //搜索结果记录数
            double dMin = 0;
            double dMax = 0;
            if (radio_String.Checked == true)
            {
                if (cmbValue.Text.Trim().Length == 0)
                {
                    MsgHelper.ShowInfomationMsgbox("请先输入检索值!");
                    return;
                }
            }
            else
            {
                if ((txt_MinNum.Text.Trim().Length == 0) || (txt_MaxNum.Text.Trim().Length == 0))
                {
                    MsgHelper.ShowInfomationMsgbox("请先输入检索数值的范围！");
                    return;
                }

                if ((Double.TryParse(txt_MinNum.Text.Trim(), out dMin) == false) || (Double.TryParse(txt_MaxNum.Text.Trim(), out dMax) == false))
                {
                    MsgHelper.ShowInfomationMsgbox("请检查数值范围的上下限是否输入数值类型！");
                    return;
                }
                if (dMin > dMax)
                {
                    double dTemp = dMin;
                    dMin = dMax;
                    dMax = dTemp;
                }
            }

            try
            {
                this.Cursor = Cursors.WaitCursor;
                IFeatureClass mFcls;
                IMap pMap = (m_Application.Document as IMxDocument).ActiveView.FocusMap ;
                int fld_index = 0;
                Grid_Result.Rows.Clear();

                if (ValueList.Contains(cmbValue.Text.Trim()) == false)
                {
                    ValueList.Add(cmbValue.Text.Trim());
                    cmbValue.Items.Add(cmbValue.Text.Trim());
                }

                object obj;
                int index = 0;
                
                IQueryFilter pFilter = new QueryFilterClass();

                List<IFeatureLayer> FeatureLayerList = new List<IFeatureLayer>();
                if (cmbDomain.SelectedIndex == 0)   //所有图层
                {
                    IEnumLayer layers = pMap.get_Layers(null, true);
                    ILayer layer = layers.Next();
                    while (layer != null)
                    {
                        if (layer.Valid && (layer is IFeatureLayer))
                        {
                            FeatureLayerList.Add(layer as IFeatureLayer);
                        }
                        layer = layers.Next();
                    }
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(layers);
                }
                else if (cmbDomain.SelectedIndex == 1)   //可见图层
                {
                    IEnumLayer layers = pMap.get_Layers(null, true);
                    ILayer layer = layers.Next();
                    while (layer != null)
                    {
                        if (layer.Valid && (layer is IFeatureLayer) && layer.Visible)
                        {
                            FeatureLayerList.Add(layer as IFeatureLayer);
                        }
                        layer = layers.Next();
                    }
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(layers);
                }
                else
                {
                    FeatureLayerList.Add(m_FeatureLayerList[cmbDomain.SelectedIndex - 2]);
                }

                if (FeatureLayerList.Count == 0) return;

                for (int m = 0; m < FeatureLayerList.Count; m++)
                {
                    if (bStop) break;
                    mFcls = FeatureLayerList[m].FeatureClass;
                    lab_Info.Text = "检索图层:" + mFcls.AliasName;

                    if (cmbField.Text == "所有字段")
                    {
                        #region 所有字段
                        for (int i = 0; i < mFcls.Fields.FieldCount; i++)
                        {
                            if (bStop) break;
                            pFilter.WhereClause = "";

                            if (radio_String.Checked)
                            {
                                if (mFcls.Fields.get_Field(i).Type == esriFieldType.esriFieldTypeString)
                                {
                                    pFilter.SubFields = mFcls.OIDFieldName + ", " + mFcls.Fields.get_Field(i).Name;
                                    if (cmbMatch.SelectedIndex == 0)
                                    {
                                        pFilter.WhereClause = mFcls.Fields.get_Field(i).Name + " like '" + cmbValue.Text.Trim() + "%'";
                                    }
                                    else if (cmbMatch.SelectedIndex == 1)
                                    {
                                        pFilter.WhereClause = mFcls.Fields.get_Field(i).Name + " like '%" + cmbValue.Text.Trim() + "%'";
                                    }
                                    else if (cmbMatch.SelectedIndex == 2)
                                    {
                                        pFilter.WhereClause = mFcls.Fields.get_Field(i).Name + " = '" + cmbValue.Text.Trim() + "'";
                                    }
                                }
                            }
                            else
                            {
                                if ((mFcls.Fields.get_Field(i).Type == esriFieldType.esriFieldTypeDouble) || (mFcls.Fields.get_Field(i).Type == esriFieldType.esriFieldTypeSingle))
                                {
                                    pFilter.SubFields = mFcls.OIDFieldName + ", " + mFcls.Fields.get_Field(i).Name;
                                    pFilter.WhereClause = mFcls.Fields.get_Field(i).Name + " >= " + dMin.ToString() + " and " + mFcls.Fields.get_Field(i).Name + " <= " + dMax.ToString();
                                }
                                else if ((mFcls.Fields.get_Field(i).Type == esriFieldType.esriFieldTypeInteger) || (mFcls.Fields.get_Field(i).Type == esriFieldType.esriFieldTypeSmallInteger) || (mFcls.Fields.get_Field(i).Type == esriFieldType.esriFieldTypeOID))
                                {
                                    pFilter.SubFields = mFcls.OIDFieldName + ", " + mFcls.Fields.get_Field(i).Name;
                                    pFilter.WhereClause = mFcls.Fields.get_Field(i).Name + " >= " + Convert.ToString((int)dMin) + " and " + mFcls.Fields.get_Field(i).Name + " <= " + Convert.ToString((int)dMax);
                                }
                            }
                            if (pFilter.WhereClause.Length > 4)
                            {
                                System.Windows.Forms.Application.DoEvents();
                                if (bStop) break;

                                IFeatureCursor pCursor = mFcls.Search(pFilter, false);
                                IFeature pFeature = pCursor.NextFeature();

                                while (pFeature != null)
                                {
                                    obj = pFeature.get_Value(i);

                                    if (obj != DBNull.Value)
                                    {
                                        Grid_Result.Rows.Add(1);
                                        Grid_Result.Rows[index].Cells[0].Value = obj.ToString();
                                        Grid_Result.Rows[index].Cells[1].Value = mFcls.AliasName.ToString();
                                        Grid_Result.Rows[index].Cells[2].Value = mFcls.Fields.get_Field(i).Name.ToString();
                                        Grid_Result.Rows[index].Tag = new FeatureClassOID(mFcls, pFeature.OID);
                                        index++;
                                        ResultCount++;
                                        if (ResultCount > 500)
                                        {
                                            lab_Info.Text = "检索结果超过500条。";
                                            bStop = true;
                                            break;
                                        }
                                    }
                                    pFeature = pCursor.NextFeature();
                                }

                                System.Runtime.InteropServices.Marshal.ReleaseComObject(pCursor);
                            }
                        }
                        #endregion
                    }
                    else if (cmbField.Text == "所有字段(非系统字段)")
                    {
                        #region 所有字段(非系统字段)
                        for (int i = 0; i < mFcls.Fields.FieldCount; i++)
                        {
                            if (bStop) break;
                            pFilter.WhereClause = "";

                            if (radio_String.Checked)
                            {
                                if (mFcls.Fields.get_Field(i).Type == esriFieldType.esriFieldTypeString)
                                {
                                    pFilter.SubFields = mFcls.OIDFieldName + ", " + mFcls.Fields.get_Field(i).Name;
                                    if (cmbMatch.SelectedIndex == 0)
                                    {
                                        pFilter.WhereClause = mFcls.Fields.get_Field(i).Name + " like '" + cmbValue.Text.Trim() + "%'";
                                    }
                                    else if (cmbMatch.SelectedIndex == 1)
                                    {
                                        pFilter.WhereClause = mFcls.Fields.get_Field(i).Name + " like '%" + cmbValue.Text.Trim() + "%'";
                                    }
                                    else if (cmbMatch.SelectedIndex == 2)
                                    {
                                        pFilter.WhereClause = mFcls.Fields.get_Field(i).Name + " = '" + cmbValue.Text.Trim() + "'";
                                    }
                                }
                            }
                            else
                            {
                                if ( mFcls.LengthField!=null && mFcls.Fields.get_Field(i).Name == mFcls.LengthField.Name) continue;
                                if (mFcls.AreaField!=null &&  mFcls.Fields.get_Field(i).Name == mFcls.AreaField.Name) continue;
                                if ((mFcls.Fields.get_Field(i).Type == esriFieldType.esriFieldTypeDouble) || (mFcls.Fields.get_Field(i).Type == esriFieldType.esriFieldTypeSingle))
                                {
                                    pFilter.SubFields = mFcls.OIDFieldName + ", " + mFcls.Fields.get_Field(i).Name;
                                    pFilter.WhereClause = mFcls.Fields.get_Field(i).Name + " >= " + dMin.ToString() + " and " + mFcls.Fields.get_Field(i).Name + " <= " + dMax.ToString();
                                }
                                else if ((mFcls.Fields.get_Field(i).Type == esriFieldType.esriFieldTypeInteger) || (mFcls.Fields.get_Field(i).Type == esriFieldType.esriFieldTypeSmallInteger))
                                {
                                    pFilter.SubFields = mFcls.OIDFieldName + ", " + mFcls.Fields.get_Field(i).Name;
                                    pFilter.WhereClause = mFcls.Fields.get_Field(i).Name + " >= " + Convert.ToString((int)dMin) + " and " + mFcls.Fields.get_Field(i).Name + " <= " + Convert.ToString((int)dMax);
                                }
                            }
                            if (pFilter.WhereClause.Length > 4)
                            {
                                System.Windows.Forms.Application.DoEvents();
                                if (bStop) break;

                                IFeatureCursor pCursor = mFcls.Search(pFilter, false);
                                IFeature pFeature = pCursor.NextFeature();

                                while (pFeature != null)
                                {
                                    obj = pFeature.get_Value(i);

                                    if (obj != DBNull.Value)
                                    {
                                        Grid_Result.Rows.Add(1);
                                        Grid_Result.Rows[index].Cells[0].Value = obj.ToString();
                                        Grid_Result.Rows[index].Cells[1].Value = mFcls.AliasName.ToString();
                                        Grid_Result.Rows[index].Cells[2].Value = mFcls.Fields.get_Field(i).Name.ToString();
                                        Grid_Result.Rows[index].Tag = new FeatureClassOID(mFcls, pFeature.OID);
                                        index++;
                                        ResultCount++;
                                        if (ResultCount > 500)
                                        {
                                            lab_Info.Text = "检索结果超过500条。";
                                            bStop = true;
                                            break;
                                        }
                                    }
                                    pFeature = pCursor.NextFeature();
                                }

                                System.Runtime.InteropServices.Marshal.ReleaseComObject(pCursor);
                            }
                        }
                        #endregion
                    }
                    else //指定字段
                    {
                        #region 具体某一个字段
                        fld_index = mFcls.Fields.FindField(cmbField.Text.Trim());
                        if (fld_index < 0) continue;

                        pFilter.WhereClause = "";

                        if (radio_String.Checked)
                        {
                            if (mFcls.Fields.get_Field(fld_index).Type == esriFieldType.esriFieldTypeString)
                            {
                                pFilter.SubFields = mFcls.OIDFieldName + ", " + mFcls.Fields.get_Field(fld_index).Name;
                                if (cmbMatch.SelectedIndex == 0)
                                {
                                    pFilter.WhereClause = mFcls.Fields.get_Field(fld_index).Name + " like '" + cmbValue.Text.Trim() + "%'";
                                }
                                else if (cmbMatch.SelectedIndex == 1)
                                {
                                    pFilter.WhereClause = mFcls.Fields.get_Field(fld_index).Name + " like '%" + cmbValue.Text.Trim() + "%'";
                                }
                                else if (cmbMatch.SelectedIndex == 2)
                                {
                                    pFilter.WhereClause = mFcls.Fields.get_Field(fld_index).Name + " = '" + cmbValue.Text.Trim() + "'";
                                }
                            }
                        }
                        else
                        {
                            if ((mFcls.Fields.get_Field(fld_index).Type == esriFieldType.esriFieldTypeDouble) || (mFcls.Fields.get_Field(fld_index).Type == esriFieldType.esriFieldTypeSingle))
                            {
                                pFilter.SubFields = mFcls.OIDFieldName + ", " + mFcls.Fields.get_Field(fld_index).Name;
                                pFilter.WhereClause = mFcls.Fields.get_Field(fld_index).Name + " >= " + dMin.ToString() + " and " + mFcls.Fields.get_Field(fld_index).Name + " <= " + dMax.ToString();
                            }
                            else if ((mFcls.Fields.get_Field(fld_index).Type == esriFieldType.esriFieldTypeInteger) || (mFcls.Fields.get_Field(fld_index).Type == esriFieldType.esriFieldTypeSmallInteger) || (mFcls.Fields.get_Field(fld_index).Type == esriFieldType.esriFieldTypeOID))
                            {
                                pFilter.SubFields = mFcls.OIDFieldName + ", " + mFcls.Fields.get_Field(fld_index).Name;
                                pFilter.WhereClause = mFcls.Fields.get_Field(fld_index).Name + " >= " + Convert.ToString((int)dMin) + " and " + mFcls.Fields.get_Field(fld_index).Name + " <= " + Convert.ToString((int)dMax);
                            }
                        }

                        if (pFilter.WhereClause.Length > 4)
                        {
                            System.Windows.Forms.Application.DoEvents();
                            if (bStop) break;

                            IFeatureCursor pCursor = mFcls.Search(pFilter, false);
                            IFeature pFeature = pCursor.NextFeature();
                            while (pFeature != null)
                            {
                                obj = pFeature.get_Value(fld_index);
                                if (obj != DBNull.Value)
                                {
                                    Grid_Result.Rows.Add(1);
                                    Grid_Result.Rows[index].Cells[0].Value = obj.ToString();
                                    Grid_Result.Rows[index].Cells[1].Value = mFcls.AliasName.ToString();
                                    Grid_Result.Rows[index].Cells[2].Value = cmbField.Text.ToString();
                                    Grid_Result.Rows[index].Tag = new FeatureClassOID(mFcls, pFeature.OID);
                                    index++;
                                    ResultCount++;
                                    if (ResultCount > 500)
                                    {
                                        lab_Info.Text = "检索结果超过500条。";
                                        bStop = true;
                                        break;
                                    }
                                }
                                pFeature = pCursor.NextFeature();
                            }
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(pCursor);
                        }
                        #endregion
                    }
                }
            }
            catch (System.Exception)
            {
            }
            finally
            {
                if (ResultCount < 500)
                {
                    lab_Info.Text = "检索完毕！";
                }
                this.Cursor = Cursors.Default;
            }
        }

        private void FrmQueryAllLayer_Load(object sender, EventArgs e)
        {
            try
            {
                //cmbDomain.SelectedIndex = 0;
                //cmbField.SelectedIndex = 0;
                cmbMatch.SelectedIndex = 0;
                radio_String.Checked = true;
                cmbValue.Focus();
                for (int i = 0; i < JHBHelper.ScaleValues.Count; i++)
                {
                    cbxScale.Items.Add(JHBHelper.ScaleValues[i].ToString());
                }
                cbxScale.SelectedIndex = 0;
            }
            catch 
            {
            }
        }

        private void btn_Stop_Click(object sender, EventArgs e)
        {
            bStop = true;
        }

        /// <summary>
        /// 检索结果列表中右键点击，点击点所在行被选中，同时显示菜单"移动到该地物"或"缩放到该地物",
        /// 移动或缩放到该地物后，该地物闪烁显示。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Grid_Result_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                try
                {
                    int RowIndex = Grid_Result.HitTest(e.X, e.Y).RowIndex;
                    if (RowIndex > -1)
                    {
                        for (int i = Grid_Result.SelectedRows.Count - 1; i > -1; i--)
                        {
                            Grid_Result.SelectedRows[i].Selected = false;
                        }
                        Grid_Result.Rows[RowIndex].Selected = true;
                    }
                    if (Grid_Result.SelectedRows.Count == 0)
                        return;
                    FeatureClassOID pFeatClsOID = (FeatureClassOID) Grid_Result.SelectedRows[0].Tag;
                    IFeatureClass mFcls = pFeatClsOID.FeatCls;
                    int OID = pFeatClsOID.OID;
                    IFeature pFeature = mFcls.GetFeature(OID);
                    
                    //增加右键菜单
                    ContextMenuStrip tocMenuStrip = new ContextMenuStrip();
                                        
                    ToolStripMenuItem itemZoom = new ToolStripMenuItem("缩放到该地物");
                    itemZoom.Click += delegate(object sender1, EventArgs e1)
                    {
                        ZoomToGeometry(pFeature.Shape);
                        FlashHelper.FlashGeometry(pFeature.Shape, (this.m_Application as IFramework).MapControl);
                    };
                    ToolStripMenuItem itemMove = new ToolStripMenuItem("移动到该地物");
                    itemMove.Click += delegate(object sender1, EventArgs e1)
                    {
                        MoveToGeometry(pFeature.Shape);
                        FlashHelper.FlashGeometry(pFeature.Shape, (this.m_Application as IFramework).MapControl);
                    };
                    ToolStripMenuItem itemSelect = new ToolStripMenuItem("选中该地物");
                    itemSelect.Click += delegate(object sender1, EventArgs e1)
                    {
                        IMap m_Map = (this.m_Application.Document as IMxDocument).ActiveView.FocusMap;
                        ILayer pSelLayer = FindLayerByFeatureClass(ref m_Map, ref mFcls);

                        if (pSelLayer != null)
                        {
                            m_Map.SelectFeature(pSelLayer, pFeature);
                            (this.m_Application.Document as IMxDocument).ActiveView.Refresh();
                        }
                    };

                    tocMenuStrip.Items.AddRange(new ToolStripItem[]
                                                {
                                                    itemZoom,
                                                    itemMove,
                                                    itemSelect
                                                });
                    tocMenuStrip.Show(this.Grid_Result, e.X, e.Y);
                }
                catch (Exception ce)
                {
                    MsgHelper.ShowErrorMsgbox(ce.Message);
                }
            }
        }

        /// <summary>
        /// 移动到图形
        /// </summary>
        /// <param name="pGeometry"></param>
        private void MoveToGeometry(IGeometry pGeometry)
        {
            IPoint pPoint;
            if (pGeometry.GeometryType == esriGeometryType.esriGeometryPoint)
            {
                pPoint = pGeometry as IPoint;
            }
            else if (pGeometry.GeometryType == esriGeometryType.esriGeometryPolygon)
            {
                IArea pArea = pGeometry as IArea;
                pPoint = pArea.LabelPoint;
            }
            else
            {
                IEnvelope pEnvelop = pGeometry.Envelope;
                pPoint = new PointClass();
                pPoint.X = pEnvelop.XMin + pEnvelop.Width / 2;
                pPoint.Y = pEnvelop.YMin + pEnvelop.Height / 2;
                pPoint.Z = 0;
            }

            IActiveView pActiveView = (this.m_Application.Document as IMxDocument).ActiveView;
            IDisplayTransformation pDisplayTransform = pActiveView.ScreenDisplay.DisplayTransformation;
            IEnvelope pEnvelope = pDisplayTransform.VisibleBounds;

            pEnvelope.CenterAt(pPoint);
            pDisplayTransform.VisibleBounds = pEnvelope;

            pActiveView.Refresh();

            //让移动完成后，再进行下一步操作，如闪烁
            pActiveView.ScreenDisplay.UpdateWindow();
        }

        /// <summary>
        /// 缩放到图形 
        /// </summary>
        /// <param name="pGeometry"></param>
        private void ZoomToGeometry(IGeometry pGeometry)
        {
           // double PtZoomScale = Convert.ToDouble(combo_Scale.Text.Substring(2));
            double PtZoomScale = Convert.ToDouble(cbxScale.Text);
            IActiveView pActiveView = (this.m_Application.Document as IMxDocument).ActiveView;
            (this.m_Application as IFramework).MapControl.Map.MapScale = PtZoomScale;
            if (pGeometry.GeometryType == esriGeometryType.esriGeometryPoint)
            {
                (this.m_Application as IFramework).MapControl.CenterAt(pGeometry as IPoint);
            }
            else
            {
                IEnvelope pEnvelop = pGeometry.Envelope;
                IPoint ptCenter = new PointClass();
                ptCenter.X = (pEnvelop.XMax + pEnvelop.XMin) / 2;
                ptCenter.Y = (pEnvelop.YMax + pEnvelop.YMin) / 2;
                ptCenter.Z = pEnvelop.ZMin;
                (this.m_Application as IFramework).MapControl.CenterAt(ptCenter);
            }

            pActiveView.Refresh();

            //让缩放完成后，再进行下一步操作，如闪烁
            pActiveView.ScreenDisplay.UpdateWindow();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbValue_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btn_Search_Click(null, null);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radio_String_CheckedChanged(object sender, EventArgs e)
        {
            SetInputState(radio_String.Checked);
            if (cmbDomain.Items.Count == 0)
            {
                cmbDomain.Items.Clear();
                cmbDomain.Items.Add("所有图层");
                cmbDomain.Items.Add("可见图层");

                m_FeatureLayerList.Clear();
                IEnumLayer layers = (this.m_Application.Document as IMxDocument).ActiveView.FocusMap.get_Layers(null, true);
                ILayer layer = layers.Next();
                while (layer != null)
                {
                    if (layer.Valid && (layer is IFeatureLayer))
                    {
                        m_FeatureLayerList.Add(layer as IFeatureLayer);
                        cmbDomain.Items.Add(layer.Name);
                    }
                    layer = layers.Next();
                }
                System.Runtime.InteropServices.Marshal.ReleaseComObject(layers);
                if (cmbDomain.Items.Count > 2)
                    cmbDomain.SelectedIndex = 2;
            }
        }

        /// <summary>
        /// 文字或数字切换
        /// </summary>
        /// <param name="bString"></param>
        private void SetInputState(bool bString)
        {
            cmbValue.Visible = bString;
            cmbMatch.Enabled = bString;
            txt_MaxNum.Visible = txt_MinNum.Visible = label6.Visible = !bString;
            if (bString)
            {
                label1.Text = "检 索 值:";
            }
            else
            {
                label1.Text = "数值范围:";
            }
        }

        /// <summary>
        /// 鼠标左键双击，对当前所点击地物进行闪烁显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Grid_Result_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                try
                {
                    FeatureClassOID pFeatClsOID = (FeatureClassOID) Grid_Result.SelectedRows[0].Tag;
                    IFeatureClass mFcls = pFeatClsOID.FeatCls;
                    int OID = pFeatClsOID.OID;
                    IFeature pFeature = mFcls.GetFeature(OID);
                    ZoomToGeometry(pFeature.Shape);
                    FlashHelper.FlashGeometry(pFeature.Shape, (this.m_Application as IFramework).MapControl);
                }
                catch (Exception ce)
                {
                    MsgHelper.ShowErrorMsgbox(ce.Message);
                }
            }
        }

        /// <summary>
        /// 输入浮点数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Num_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 46 && e.KeyChar != 110 && e.KeyChar != 45)
                e.Handled = true;
            if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
                e.Handled = false;
        }

        /// <summary>
        /// 要素类检索所在图层
        /// </summary>
        /// <param name="pMap"></param>
        /// <param name="pFeatCls"></param>
        /// <returns></returns>
        public static ILayer FindLayerByFeatureClass(ref IMap pMap, ref IFeatureClass pFeatCls)
        {
            if (pFeatCls == null)
            {
                return null;
            }

            IList<IFeatureLayer> pFeatLayers = MapHelper.GetAllLayer<IFeatureLayer>(pMap);
            if ((pFeatLayers == null) || (pFeatLayers.Count <= 0))
            {
                return null;
            }

            ILayer pFindLayer = null;
            IFeatureLayer pFeatLayer = null;
            IEnumerator<IFeatureLayer> pFeatLayerEnum = null;
            IDataset pDataset = null;
            IWorkspace pWsk1 = null;
            IWorkspace pWsk2 = null;
            bool bSame = false;

            pDataset = pFeatCls as IDataset;
            pWsk1 = pDataset.Workspace as IWorkspace;

            pFeatLayerEnum = pFeatLayers.GetEnumerator();
            while (pFeatLayerEnum.MoveNext())
            {
                pFeatLayer = pFeatLayerEnum.Current;

                pDataset = pFeatLayer.FeatureClass as IDataset;
                pWsk2 = pDataset.Workspace as IWorkspace;

                bSame = pWsk2.Equals2(pWsk1);
                bSame = bSame && (pFeatLayer.FeatureClass.ObjectClassID == pFeatCls.ObjectClassID);
                bSame = bSame && (pFeatLayer.FeatureClass.AliasName.Equals(pFeatCls.AliasName)); //key 

                if (bSame)
                {
                    pFindLayer = pFeatLayer as ILayer;
                    break;
                }
            }
            return pFindLayer;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbDomain_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbField.Items.Clear();
            cmbField.Items.Add("所有字段");
            
            int j = 0;
            IField pField;
            if (radio_String.Checked)
            {
                if (cmbDomain.SelectedIndex == 0)
                {
                    List<string> FieldNames = new List<string>();
                    for (int i = 0; i < m_FeatureLayerList.Count; i++)
                    {
                        for (j = 0; j < m_FeatureLayerList[i].FeatureClass.Fields.FieldCount; j++)
                        {
                            pField = m_FeatureLayerList[i].FeatureClass.Fields.get_Field(j);
                            if (pField.Type == esriFieldType.esriFieldTypeString)
                                if (FieldNames.Contains(pField.Name) == false)
                                    FieldNames.Add(pField.Name);
                        }
                    }

                    for (int i = 0; i < FieldNames.Count; i++)
                    {
                        cmbField.Items.Add(FieldNames[i]);
                    }
                }
                else if (cmbDomain.SelectedIndex == 1)
                {
                    List<string> FieldNames = new List<string>();
                    for (int i = 0; i < m_FeatureLayerList.Count; i++)
                    {
                        if (m_FeatureLayerList[i].Valid && m_FeatureLayerList[i].Visible)
                        {
                            for (j = 0; j < m_FeatureLayerList[i].FeatureClass.Fields.FieldCount; j++)
                            {
                                pField = m_FeatureLayerList[i].FeatureClass.Fields.get_Field(j);
                                if (pField.Type == esriFieldType.esriFieldTypeString)
                                    if (FieldNames.Contains(pField.Name) == false)
                                        FieldNames.Add(pField.Name);
                            }
                        }
                    }

                    for (int i = 0; i < FieldNames.Count; i++)
                    {
                        cmbField.Items.Add(FieldNames[i]);
                    }
                }
                else
                {
                    for (j = 0; j < m_FeatureLayerList[cmbDomain.SelectedIndex - 2].FeatureClass.Fields.FieldCount; j++)
                    {
                        pField = m_FeatureLayerList[cmbDomain.SelectedIndex - 2].FeatureClass.Fields.get_Field(j);
                        if (pField.Type == esriFieldType.esriFieldTypeString)
                            cmbField.Items.Add(pField.Name);
                    }
                }
            }
            else
            {
                cmbField.Items.Add("所有字段(非系统字段)");
                if (cmbDomain.SelectedIndex == 0)
                {
                    List<string> FieldNames = new List<string>();
                    for (int i = 0; i < m_FeatureLayerList.Count; i++)
                    {
                        for (j = 0; j < m_FeatureLayerList[i].FeatureClass.Fields.FieldCount; j++)
                        {
                            pField = m_FeatureLayerList[i].FeatureClass.Fields.get_Field(j);
                            if ((pField.Type == esriFieldType.esriFieldTypeSmallInteger) || (pField.Type == esriFieldType.esriFieldTypeInteger) || (pField.Type == esriFieldType.esriFieldTypeSingle)  || (pField.Type == esriFieldType.esriFieldTypeDouble))
                                if (FieldNames.Contains(pField.Name) == false)
                                    FieldNames.Add(pField.Name);
                        }
                    }

                    for (int i = 0; i < FieldNames.Count; i++)
                    {
                        cmbField.Items.Add(FieldNames[i]);
                    }
                }
                else if (cmbDomain.SelectedIndex == 1)
                {
                    List<string> FieldNames = new List<string>();
                    for (int i = 0; i < m_FeatureLayerList.Count; i++)
                    {
                        if (m_FeatureLayerList[i].Valid && m_FeatureLayerList[i].Visible)
                        {
                            for (j = 0; j < m_FeatureLayerList[i].FeatureClass.Fields.FieldCount; j++)
                            {
                                pField = m_FeatureLayerList[i].FeatureClass.Fields.get_Field(j);
                                if ((pField.Type == esriFieldType.esriFieldTypeSmallInteger) || (pField.Type == esriFieldType.esriFieldTypeInteger) || (pField.Type == esriFieldType.esriFieldTypeSingle) || (pField.Type == esriFieldType.esriFieldTypeDouble))
                                    if (FieldNames.Contains(pField.Name) == false)
                                        FieldNames.Add(pField.Name);
                            }
                        }
                    }

                    for (int i = 0; i < FieldNames.Count; i++)
                    {
                        cmbField.Items.Add(FieldNames[i]);
                    }
                }
                else
                {
                    for (j = 0; j < m_FeatureLayerList[cmbDomain.SelectedIndex - 2].FeatureClass.Fields.FieldCount; j++)
                    {
                        pField = m_FeatureLayerList[cmbDomain.SelectedIndex - 2].FeatureClass.Fields.get_Field(j);
                        if ((pField.Type == esriFieldType.esriFieldTypeSmallInteger) || (pField.Type == esriFieldType.esriFieldTypeInteger) || (pField.Type == esriFieldType.esriFieldTypeSingle) || (pField.Type == esriFieldType.esriFieldTypeDouble))
                            cmbField.Items.Add(pField.Name);
                    }
                }
            }
            cmbField.SelectedIndex = 0;
        }
        
    }

    public struct FeatureClassOID
    {
        public IFeatureClass FeatCls;
        public int OID;

        public FeatureClassOID(IFeatureClass vFeatCls, int vOID)
        {
            FeatCls = vFeatCls;
            OID = vOID;
        }
    }
}
